package com.eascs.pbafterloan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.api.form.O2oAfterLoanForm;
import com.eascs.afterloan.dao.O2oReportAfterDao;
import com.eascs.afterloan.dao.OaAfterDao;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.pbafterloan.dao.O2oPBAfterDao;

@Service
public class O2oPBAfterService {

	@Autowired
	O2oPBAfterDao o2oAfterDao;
	@Autowired
	OaAfterDao oaAfterDao;
	
	@Autowired
	O2oReportAfterDao o2oReportAfterDao;
	
	public PageResult<Object[]> getO2oAfterLoanByPageNew(O2oAfterLoanForm entity){
		PageResult<Object[]> pageResults = o2oAfterDao.getO2oAfterLoanByPageNew(entity);
		return pageResults;
	}
	
	
	public PageResult<Object[]> getO2oAfterLoanExport(O2oAfterLoanForm entity){
		PageResult<Object[]> pageResults = o2oAfterDao.getO2oAfterLoanExport(entity);
		return pageResults;
	}
	
	public PageResult<Object[]> getO2oAfterLoanByPage(O2oAfterLoanForm entity){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getO2oIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT OL.BILL_CODE,OL.BANK_LOAN_NAME,OL.CUSTOME_RNAME,OL.LTD_NAME,OLU.USER_FULL_NAME,OL.REPAY_MENTWAY ");
		querySql.append(" ,FORMAT(OL.LOANRATE*100,2),OL.LOAN_TERM,OL.PAYMENT_TIME,OL.LOANA_MOUNT,OL.EA_MOUNT,OL.OPERATOR_NAME,OL.CREATE_DATE,OL.BANK_TYPE,OL.WF_TYPE ");
		querySql.append(" FROM O2O_LOANAPPLY OL JOIN O2O_PAYMENTS_PLAN OPP ON OL.BILL_CODE = OPP.BILL_CODE ");
		querySql.append(" JOIN O2O_LOAN_USER OLU ON OLU.LOANAPPLY_NO = OL.LOANAPPLY_NO AND OL.LOAN_ID = OLU.LOAN_ID WHERE OPP.IS_DEL='N' ");
		if(!StringUtil.isEmpty(entity.getBillCode())){
			querySql.append(" AND OL.BILL_CODE LIKE ?");
			sqlPageQuery.addLikeParam(entity.getBillCode());
		}
		if(!StringUtil.isEmpty(entity.getBankLoan())){
			querySql.append(" AND OL.BANK_LOAN_NAME LIKE ?");
			sqlPageQuery.addLikeParam(entity.getBankLoan());
		}
		if(!StringUtil.isEmpty(entity.getOperName())){
			querySql.append(" AND OL.OPERATOR_NAME LIKE ?");
			sqlPageQuery.addLikeParam(entity.getOperName());
		}
		if(!StringUtil.isEmpty(entity.getLoanUserName())){
			querySql.append(" AND OL.USER_FULL_NAME LIKE ?");
			sqlPageQuery.addLikeParam(entity.getLoanUserName());
		}
		
		/*if(!StringUtil.isEmpty(entity.getLoanDate())){
			querySql.append(" AND OL.PAYMENT_TIME LIKE ?");
			sqlPageQuery.addLikeParam(entity.getLoanDate());
		}
		if(!StringUtil.isEmpty(entity.getCreateDate())){
			querySql.append(" AND OL.CREATE_DATE LIKE ?");
			sqlPageQuery.addLikeParam(entity.getCreateDate());
		}
		if(!StringUtil.isEmpty(entity.getLtdName())){
			querySql.append(" AND OL.LTD_NAME LIKE ?");
			sqlPageQuery.addLikeParam(entity.getCreateDate());
		}*/
		querySql.append(" GROUP BY OL.BILL_CODE ORDER BY OL.CREATE_DATE DESC ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return o2oAfterDao.findDataWithPage(sqlPageQuery);
	}
	
	public PageResult<Object[]> getOaAfterLoanByPage(O2oAfterLoanForm entity){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getOaIndex());
		StringBuffer querySql = new StringBuffer();
		
		
		
		sqlPageQuery.setQuerySql(querySql.toString());
		return oaAfterDao.findDataWithPage(sqlPageQuery);
	}
}
